<?php

namespace App\Models\Admin;
//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 后台用户表
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

use App\Tools\Financial;
use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;

class Express extends Model
{

    protected $table = 'express';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 获取列表
     * @param $where
     * @param int $limit
     * @return mixed
     */
    public static function getPageList($where, $limit = 15)
    {
        $data = [];
        if ($where['name']) {
            $data[] = ['express_name', 'like', "%{$where['name']}%"];
        }
        if ($limit == 0) {
            $list = self::where($data)->get();
        } else {
            $list = self::where($data)->paginate($limit);
        }
        foreach ($list as $key => $vv) {
            $list[$key]['status'] = Prstatic::$status[$vv['status']];
        }
        return $list;
    }

    /**
     * 获取数据
     * @return mixed
     */
    public static function getAllList()
    {
//        return self::where(['status' => 1])->select(['id', 'express_name', 'area_list', 'order_money'])->get();
        $list = self::where(['status' => 1])->select(['id', 'express_name', 'area_list', 'order_money'])->get();
        foreach ($list as $key => $item) {
            $list[$key]['area_list'] = $item['area_list'] ? json_decode($item['area_list'], true) : [];
        }
        return $list;
    }

    /**
     * 获取数据
     * @return mixed
     */
    public static function getNameById($id)
    {
        return self::where(['id' => $id])->value('express_name');
    }

    /**
     * 获取运费
     * @param $id
     * @param $num
     * @param $money
     * @param $order_province
     * @return float|int|mixed
     */
    public static function getExpressFee($id, $num, $money, $order_province)
    {
        $info = self::where(['id' => $id])->select(['id', 'express_name', 'area_list', 'order_money'])->first();
        if ($info) {
            $area_list = json_decode($info['area_list'], true);
            if ($money < $info['order_money']) {
                foreach ($area_list as $key => $vv) {
                    if ($vv['area_name'] == $order_province) {
                        $expressFee = $num * $vv['two_unit'] + $vv['first_unit'] - $vv['two_unit'];
                        break;
                    }
                }
            }
            return $expressFee;
        }
        return 0;
    }


    /**
     * 获取信息
     * @param $id
     */
    public static function getInfoById($id)
    {
        $info = self::where(['id' => $id])->first();
        if ($info) {
            $info['area_list'] = $info['area_list'] ? json_decode($info['area_list'], true) : [];
            return $info->toArray();
        }
        return [];
    }


    /**
     * 添加快递
     */
    public static function addOrUpInfo($dataArr)
    {
        $info['express_name'] = $dataArr['express_name'];
        $info['money'] = $dataArr['money'] ?: 0;
        $info['order_money'] = $dataArr['order_money'] ?: 0;
        $info['remark'] = $dataArr['remark'] ?: '';
        $info['area_list'] = $dataArr['areaList'] ? json_encode($dataArr['areaList'], JSON_UNESCAPED_UNICODE) : '';
        $info['status'] = $dataArr['status'] ?: 1;
        //        v($info);
        if ($dataArr['id']) {
            return self::where(['id' => $dataArr['id']])->update($info);
        } else {
            $info['created_at'] = time();
            return self::insert($info);
        }
    }
}